﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using IBS;
namespace HQCSupplierLoader
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                //тестовая загрузка 1го файла
                string path = @"\\msk-hqdb-01.retail.tnk-bp.ru\HQCUpload\Supplier_Code\";
                string connectionString = "Data Source=ORAIBS.WIN;User ID=ibsdbo;Password=campator;";
                var files = Directory.GetFiles(path, "Supplier_Code*.txt", SearchOption.TopDirectoryOnly);


                foreach (var file in files)
                {
                    try
                    {
                        FileInfo fi = new FileInfo(file);
                        var parser = new HQCSupplierFileParser.Parser(fi.FullName);
                        var result = parser.parseFile();
                        var ibs = new IBS.IbsDataContext(connectionString);
                        ibs.DeleteHQCSupplierCode(result.FirstOrDefault().SITECODE);
                        foreach (HQCSUPPLIER_CODE line in result)
                        {
                            try
                            {
                                ibs.SaveHQCSupplierCode(line);
                            }
                            catch(Exception ex)
                            { }
                        }
                    }
                    catch (Exception ex)
                    { }

                }
            }
            catch (Exception ex)
            { }

        }
    }
}
